/* 
 * 
 * Copyright 2011 The University of Adelaide
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 *  
 */

package au.org.aekos.ands.global.populate;

import java.util.Map;
import java.util.Set;

import au.org.aekos.ands.global.Schema.EntityType;
import au.org.aekos.ands.global.Schema.Field;
import au.org.aekos.ands.global.parse.PropertyParser;

/**
 * Populates RIF-CS objects of the given type with data provided by the
 * metadata properties parsers.
 *
 * @param <T> the RIF-CS entity type.
 */
public abstract class Populate<T> {
	/**
	 * A map from each RIF-CS field type to its property parser.
	 */
	protected Map<Field,PropertyParser<?>> parsers;
	
	private EntityType type;
	
	/**
	 * Instantiates a new object for populating RIF-CS objects of the given type
	 * using data supplied by the given parsers.
	 *
	 * @param type the RIF-CS object type.
	 * @param parsers the relevant parsers.
	 */
	public Populate(EntityType type, Map<Field,PropertyParser<?>> parsers) {
		this.type = type;
		this.parsers = parsers;
	}
	
	/**
	 * Populates the specified set of fields in the given entity.
	 *
	 * @param entity the RIF-CS object.
	 * @param fields the fields to populate.
	 * @param identifier the object identifier.
	 */
	public void populate(T entity, Set<Field> fields, String identifier) {
		if ((fields == null) || fields.isEmpty()) { return; }
		for (Field field : fields) { populate(entity, field, identifier); }
	}

	/**
	 * Populates the specified field in the given entity.
	 *
	 * @param entity the RIF-CS object.
	 * @param field the field to populate.
	 * @param identifier the object identifier.
	 */
	public abstract void populate(T entity, Field field, String identifier);
	
	/**
	 * Fetches the relevant metadata for the given field and object identifier combination.
	 *
	 * @param field the field for which metadata is to be fetched.
	 * @param identifier the identifier for which metadata is to be fetched.
	 * @return an array of the metadata objects that satisfy the (field,identifier) specification.
	 */
	protected Object[] getMetadata(Field field, String identifier) {
		// System.err.println(field + ":" + identifier);
		return parsers.get(field).getMetadata(type, identifier);
	}
}
